package internationalenergytradingplatform.service.Impl;

import internationalenergytradingplatform.constant.MessageConstant;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.exception.*;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.service.SupplierAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class SupplierAccountServiceImpl implements SupplierAccountService {
    @Autowired
    private SupplierAccountDao supplierAccountDao;

    @Override
    public SupplierAccount login(SupplierAccount supplierAccountDTO){

        String tele = supplierAccountDTO.getTele();
        String password = supplierAccountDTO.getPassword();

        //1、根据手机号查找供应商账号库中的数据
        SupplierAccount supplierAccount = supplierAccountDao.getCompanyByTele(tele);

        //2、如果账号不存在
        if (supplierAccount == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对(先进行md5加密）
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(supplierAccount.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (supplierAccount.getSupplierAccountStatus() != 1) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return supplierAccount;
    }

    @Override
    public Integer register(SupplierAccount supplierAccountDTO) {
        String tele = supplierAccountDTO.getTele();
        String password = supplierAccountDTO.getPassword();
        String companyName = supplierAccountDTO.getCompanyName();
        String unifiedCreditCode= supplierAccountDTO.getUnifiedCreditCode();
        String unifiedCreditImage = supplierAccountDTO.getUnifiedCreditImage();
        String avatarImage = supplierAccountDTO.getAvatarImage();
        Integer supplierAccountStatus = 1;

        //1、判断必要属性是否填写完整
        if(tele == null || tele.trim().isEmpty()){
            //电话号为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":tele");
        }
        if(password == null || password.trim().isEmpty()){
            //密码为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":password");
        }
        if(companyName == null || companyName.trim().isEmpty()){
            //公司名为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":companyName");
        }
        if(unifiedCreditCode == null || unifiedCreditCode.trim().isEmpty()){
            //统一信用代码为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":unifiedCreditCode");
        }
        if(unifiedCreditImage == null || unifiedCreditImage.trim().isEmpty()){
            //统一信用证书为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":unifiedCreditImage");
        }
        if(avatarImage == null || avatarImage.trim().isEmpty()){
            //头像为空
            throw new MissingKeyAttributeException(MessageConstant.MISSING_KEY_ATTRIBUTES + ":avatarImage");
        }

        //2、查找手机号是否已经被注册
        SupplierAccount supplierAccount = supplierAccountDao.getCompanyByTele(tele);
        if (supplierAccount != null) {
            throw new AccountAlreadyExistsException(MessageConstant.CANNOT_REGISTER_AN_EXISTING_ACCOUNT_AGAIN);
        }

        //3、为密码md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //4、注册新的供应商
        return supplierAccountDao.addSupplierAccount(tele, password, companyName, unifiedCreditCode, unifiedCreditImage, avatarImage, supplierAccountStatus);
    }

}
